#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/11 22:02
@file: simu_login.py
"""

import requests
import ttshitu
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/97.0.4692.71 Safari/537.36'
}
base_url = 'https://so.gushiwen.cn'
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
succ_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
# 使用session
session = requests.session()
# 发送请求
login_resp = session.get(url=login_url, headers=headers).text
tree = etree.HTML(login_resp)

__view = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__gen = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
print(__view)
print(__gen)


code_img = base_url + tree.xpath('//*[@id="imgCode"]//@src')[0]
with open('code.jpg', 'wb') as fp:
    fp.write(session.get(url=code_img, headers=headers).content)

code_str = ttshitu.getImgCodeText('./code.jpg', 3)
print(code_str)

data = {
    '__VIEWSTATE': __view,
    '__VIEWSTATEGENERATOR': __gen,
    'from': 'http://so.gushiwen.cn/user/collect.aspx',
    'email': '15027900535',
    'pwd': 'bobo@15027900535',
    'code': code_str,
    'denglu': '登录',
}

succ_str = session.post(url=succ_url, headers=headers, data=data).text
with open('./result.html', 'w', encoding='utf-8') as fp:
    fp.write(succ_str)
